<template>
  <div class="content_box">
    <template v-if="!isQianKun">
      <div class="content_box_bar"></div>
      <div class="content_box_menu"></div>
    </template>
    <div :class="['content_box_main', isQianKun ? '' : 'on_qiankun']">
      <keep-alive>
        <router-view></router-view>
        <component :is="currentComponent"></component>
      </keep-alive>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      currentComponent: null,
      partNames: ['plan']
    };
  },
  computed: {
    isQianKun() {
      return window.__POWERED_BY_QIANKUN__
    }
  },
  methods: {
    loadComponentFromUrl() {
      const pathname = window.location.pathname;
      const parts = pathname.split('/');
      const urlParts = parts.slice(1, 5)
      const typeParts = parts.slice(5)
      const isPermission = this.partNames.includes(urlParts[0])
      if (isPermission) {
        const viewName = typeParts.length > 0 ? 'detail.vue' : 'index'
        import(`../views${pathname}/${viewName}`).then((module) => {
          this.currentComponent = module.default;
        });
      }else {
        //  抛出事件，打开404页面
      }
    }
  },
  created() {
    if (this.isQianKun) {
      this.loadComponentFromUrl()
      window.addEventListener('popstate', (newVal, oldVal) => {
        this.loadComponentFromUrl();
      })
    }
  }
}
</script>
<style lang="less">
.content_box {
  width: 100%;
  height: 100%;
  display: flex;
  .content_box_bar {
    width: 100%;
    position: fixed;
    left: 0;
    top: 0;
    background-color: #000;
    height: 42px;
    z-index: 4;
  }
  .content_box_menu {
    width: 200px;
    height: 100%;
    background: #000;
  }
  .content_box_main {
    flex: 1;
    height: 100%;
  }
  .content_box_main .on_qiankun {
    padding: 42px;
  }
}
</style>
